﻿using mozhi.smarterp.BasicData.Warehouses.Repositories;
using mozhi.smarterp.BasicData.Warehouses.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Guids;

namespace mozhi.smarterp.BasicData.Warehouses
{
    public class WarehouseAppService : BasicDataAppService, IWarehouseAppService
    {
        private IWarehouseRepository _repository;
        private IGuidGenerator _guidGenerator;
        private WarehouseManager _manager;

        public WarehouseAppService(IWarehouseRepository repository, IGuidGenerator guidGenerator, WarehouseManager manager)
        {
            _repository = repository;
            _guidGenerator = guidGenerator;
            _manager = manager;
        }

        public async Task<WarehouseDto> CreateAsync(WarehouseCreateDto input)
        {
            var warehouse = await _manager.GetAsync(null, input.Code, input.Name, input.Address, input.ActivateLocation, input.Activated);

            input.Locations = input.Locations ?? new List<LocationCreateDto>();

            foreach (var item in input.Locations)
            {
                var location = new Location(_guidGenerator.Create(), warehouse.Code, warehouse.Name, warehouse.Id);

                warehouse.Locations.Add(location);
            }

            warehouse = await _repository.InsertAsync(warehouse);

            return ObjectMapper.Map<Warehouse, WarehouseDto>(warehouse);
        }

        public async Task DeleteAsync(Guid id)
        {
            var warehouse = await _repository.GetAsync(id);

            if (warehouse == null)
                throw new BusinessException($"仓库：{id}不存在", "100000");

            await _repository.DeleteAsync(id);
        }

        public async Task<WarehouseDto> GetAsync(Guid id)
        {
            var warehouse = await _repository.GetAsync(id);

            return ObjectMapper.Map<Warehouse, WarehouseDto>(warehouse);
        }

        public async Task<PagedResultDto<WarehouseDto>> GetListAsync(PagedResultRequestDto filter)
        {
            var warehouses = await _repository.GetPagedListAsync(filter.SkipCount, filter.MaxResultCount, nameof(Warehouse.Code));

            var count = await _repository.GetCountAsync();

            var result = new PagedResultDto<WarehouseDto>()
            {
                Items = ObjectMapper.Map<List<Warehouse>, List<WarehouseDto>>(warehouses),
                TotalCount = count
            };

            return result;
        }

        public async Task<WarehouseDto> UpdateAsync(WarehouseUpdateDto input)
        {
            var warehouse = await _manager.GetAsync(input.Id, input.Code, input.Name, input.Address, input.ActivateLocation, input.Activated);

            input.Locations = input.Locations ?? new List<LocationUpdateDto>();

            var locations = new List<Location>();

            foreach (var item in input.Locations)
            {
                item.Id = item.Id == default ? _guidGenerator.Create() : item.Id;

                var location = new Location(item.Id, item.Code, item.Name, warehouse.Id);

                locations.Add(location);
            }

            warehouse.ChangeLocations(locations);

            warehouse = await _repository.UpdateAsync(warehouse);

            return ObjectMapper.Map<Warehouse, WarehouseDto>(warehouse);
        }
    }
}
